package com.vitabuybuy.tv.ui.home.group

import android.os.Bundle
import androidx.core.os.bundleOf
import androidx.recyclerview.widget.LinearLayoutManager
import com.flyco.tablayout.listener.CustomTabEntity
import com.flyco.tablayout.listener.OnTabSelectListener
import com.lcodecore.tkrefreshlayout.RefreshListenerAdapter
import com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout
import com.library.common.base.BaseActivity
import com.library.common.base.MessageModel
import com.library.common.net.loadingtype.LoadingType
import com.vitabuybuy.tv.R
import com.vitabuybuy.tv.databinding.ActivitySearchGroupbuyResultBinding
import com.vitabuybuy.tv.dto.ClassificationDto
import com.vitabuybuy.tv.dto.TabEntity
import com.vitabuybuy.tv.ui.home.group.adapter.GroupBuyListZoneAdapter
import com.vitabuybuy.tv.ui.home.group.viewmodel.GroupBuyZoneViewModel
import kotlinx.android.synthetic.main.activity_search_groupbuy_result.*
import java.util.*

class SearchGroupBuyResultActivity :
    BaseActivity<GroupBuyZoneViewModel, ActivitySearchGroupbuyResultBinding>() {
    override val isUseBaseTitleBar: Boolean
        get() = true
    override val layoutId: Int
        get() = R.layout.activity_search_groupbuy_result

    private var hotkey: String? = null
    private var cateBean: ClassificationDto.ChildrenBean.ChildrenBean2? = null
    private val grouplistAdapter by lazy { GroupBuyListZoneAdapter() }
    private var categoryId = -1
    private var pageNumber = 1
    private val mTabEntities = ArrayList<CustomTabEntity>()
    private lateinit var groupType: String

    override fun onGetBundle(bundle: Bundle?) {
        hotkey = bundle?.getString("hotkey")
        cateBean = bundle?.getSerializable("cateBean") as? ClassificationDto.ChildrenBean.ChildrenBean2
    }

    override fun initView(savedInstanceState: Bundle?) {
        setTitle(if (cateBean == null) hotkey!! else cateBean!!.name)
        if (cateBean != null) categoryId = cateBean!!.id
        mBinding.viewModel = viewModel
        groupType = getString(R.string.all)
        mTabEntities.add(TabEntity(getString(R.string.all), 0, 0))
        mTabEntities.add(TabEntity(getString(R.string.normal_group_buy), 0, 0))
        mTabEntities.add(TabEntity(getString(R.string.limited_group_buy), 0, 0))
        tlBiddingClass.setTabData(mTabEntities)
        listview.apply {
            adapter = grouplistAdapter
            layoutManager = LinearLayoutManager(mContext)
        }
    }

    override fun observeData() {
        viewModel.getGoodsList(
            categoryId,
            if (groupType == getString(R.string.all)) "" else groupType,
            hotkey,
            pageNumber
        )
        viewModel.goodslist.observe(this, androidx.lifecycle.Observer {
            if (pageNumber == 1) {
                grouplistAdapter.setNewData(it)
            } else {
                grouplistAdapter.addData(it)
            }
            if (grouplistAdapter.mData.isEmpty()) {
                viewModel.statusView.set(LoadingType.SHOW_EMPTY)
            }else{
                viewModel.statusView.set(LoadingType.SHOW_CONTENT)
            }
        })
    }

    override fun eventHandling() {
        super.eventHandling()
        tlBiddingClass.setOnTabSelectListener(object : OnTabSelectListener {
            override fun onTabSelect(position: Int) {
                groupType = when (position) {
                    0 -> getString(R.string.all)
                    1 -> getString(R.string.normal_group_buy)
                    2 -> getString(R.string.limited_group_buy)
                    else -> getString(R.string.all)
                }
                pageNumber = 1
                viewModel.getGoodsList(
                    categoryId,
                    if (groupType == getString(R.string.all)) "" else groupType,
                    hotkey,
                    pageNumber
                )
            }
            override fun onTabReselect(position: Int) {
            }
        })
        refresh.setOnRefreshListener(object : RefreshListenerAdapter() {
            override fun onLoadMore(refreshLayout: TwinklingRefreshLayout?) {
                super.onLoadMore(refreshLayout)
                pageNumber++
                viewModel.getGoodsList(
                    categoryId,
                    if (groupType == getString(R.string.all)) "" else groupType,
                    hotkey,
                    pageNumber
                )
            }

            override fun onRefresh(refreshLayout: TwinklingRefreshLayout?) {
                super.onRefresh(refreshLayout)
                pageNumber = 1
                viewModel.getGoodsList(
                    categoryId,
                    if (groupType == getString(R.string.all)) "" else groupType,
                    hotkey,
                    pageNumber
                )
            }
        })
        grouplistAdapter.setOnItemClickListener { binding, item, position ->
            GroupBuyDetailActivity.openActivity(mContext, item.id)
        }
    }
    override fun handleEvent(msg: MessageModel) {
        when (msg.code) {
            0 -> {
                pageNumber = 1
                viewModel.getGoodsList(
                    categoryId,
                    if (groupType == getString(R.string.all)) "" else groupType,
                    hotkey,
                    pageNumber
                )
            }
            1 -> {
                refresh.onLoad(msg.arg1)
            }
        }
    }

    companion object {
        fun openActivity(mContext: BaseActivity<*, *>,
                         cateBean: ClassificationDto.ChildrenBean.ChildrenBean2?,
                         hotkey: String?) {
            mContext.startActivity(
                bundleOf(Pair("cateBean",cateBean), Pair("hotkey", hotkey)),
                SearchGroupBuyResultActivity::class.java
            )
        }
    }
}
